package org.jypj.dev.util;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.servlet.http.HttpServletResponse;

import org.jypj.dev.util.StreamTool;

public class ImageUtil {

    public static void downloadImage(HttpServletResponse response, String imageName, String imagePath) {
        try {
            response.reset();
            response.setContentType("multipart/form-data");

            // 创建url;
            URL url = new URL(imagePath);
            // 创建url连接;
            HttpURLConnection urlconn = (HttpURLConnection) url.openConnection();
            // 链接远程服务器;
            urlconn.connect();
            // Word对应的就是 application/msword
            String fileName = new String(imageName.getBytes("UTF-8"), "ISO8859-1");
            response.setHeader("Content-disposition", "attachment; filename=" + fileName);
            imagePath = imagePath + "/" + imageName;
            BufferedInputStream bis = null;
            BufferedOutputStream bos = null;
            try {
                bis = new BufferedInputStream(urlconn.getInputStream());
                bos = new BufferedOutputStream(response.getOutputStream());

                byte[] buff = new byte[2048];
                int bytesRead;
                while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
                    bos.write(buff, 0, bytesRead);
                }
                bos.flush();

            } catch (final IOException e) {
                e.printStackTrace();
            } finally {
                if (bis != null) {
                    bis.close();
                    bis = null;
                }
                if (bos != null) {
                    bos.close();
                    bos = null;
                }
            }
            return;
        } catch (Exception e) {
        }

    }

    /**
     * @param fileUrl   文件的服务器地址
     * @param imagePath 生成的图片的地址 本地地址
     */
    public static void downloadImageToLocal(String fileUrl, String imagePath) {
        System.out.println("fileUrl=" + fileUrl);
        try {
            // 创建url;
            URL url = new URL(fileUrl);
            // 创建url连接;
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            // 链接远程服务器;
            conn.connect();
            InputStream inStream = conn.getInputStream();//通过输入流获取图片数据
            byte[] btImg = StreamTool.readInputStream(inStream);//得到图片的二进制数据
            File file = new File(imagePath);
            FileOutputStream fops = new FileOutputStream(file);
            fops.write(btImg);
            fops.flush();
            fops.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void customBufferStreamCopy(String source, String target) {
        File sourceFile = new File(source);
        File targetFile = new File(target);
        InputStream fis = null;
        OutputStream fos = null;
        try {
            fis = new FileInputStream(sourceFile);
            fos = new FileOutputStream(targetFile);
            byte[] buf = new byte[4096];
            int i;
            while ((i = fis.read(buf)) != -1) {
                fos.write(buf, 0, i);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fis.close();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
